package cc.magicjson.example.demo.web.controller;

import cc.magicjson.spring.boot.domain.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import static java.util.Objects.nonNull;

@RestController
public class HelloController {

    private final Person person;

    @Autowired
    public HelloController(Person person) {
        this.person = person;
    }

    @ResponseBody
    @GetMapping("/hello")
    public String saySpringBoot() {
        return "hello spring boot";
    }

    @GetMapping(value = "/person")
    public Person person(@RequestParam(value = "name", defaultValue = "lisi") String name) {

        if(nonNull(name)){
            person.setName(name);
        }
        return person;
    }
}
